#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

#if 0
int main() {
    // 创建 JSON 对象
    cJSON *root = cJSON_CreateObject();
    cJSON *array = cJSON_CreateArray();

    // 向 JSON 对象中添加数据
    cJSON_AddItemToObject(root, "name", cJSON_CreateString("John"));
    cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(30));

    cJSON_AddItemToArray(array, cJSON_CreateString("apple"));
    cJSON_AddItemToArray(array, cJSON_CreateString("banana"));

    cJSON_AddItemToObject(root, "fruits", array);

    // 将 JSON 对象转换为字符串并打印
    char *json_string = cJSON_Print(root);
    printf("%s\n", json_string);
    free(json_string);

    // 释放 JSON 对象
    cJSON_Delete(root);

    return 0;
}
#endif

int start() {
    
    char *json_data;
    cJSON *root, *name, *age, *fruits;

    // 从本地 JSON 文件中读取数据
    FILE *file = fopen("data.json", "r");
    if (file == NULL) {
        perror("Error opening JSON file");
        exit(EXIT_FAILURE);
    }
    printf("------------------------------------\n");

    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    json_data = (char *)malloc(file_size + 1);
    fread(json_data, 1, file_size, file);
    fclose(file);
    printf("--------------111----------------------\n");
    printf("JSON Data: %s\n", json_data);
    // 解析 JSON 数据
    root = cJSON_Parse(json_data);
    free(json_data);

    if (root == NULL) {
        printf("Error parsing JSON\n");
        exit(EXIT_FAILURE);
    } else {
        printf("JSON parsed successfully\n");
    }

    if (root == NULL) {
        printf("Error parsing JSON\n");
        exit(EXIT_FAILURE);
    }
    cJSON *person = cJSON_GetObjectItem(root, "person");
    if (person == NULL) {
        printf("Person object not found\n");
    } else{
        // 从 JSON 对象中提取数据
        name = cJSON_GetObjectItem(person, "name");
        if (name == NULL) {
            printf("Name is NULL\n");
        } else {
            printf("Name: %s\n", name->valuestring);
        }
        age = cJSON_GetObjectItem(person, "age");
        if (age == NULL) {
            printf("age is NULL\n");
        } else {
            printf("age: %d\n", age->valueint);
        }
        fruits = cJSON_GetObjectItem(person, "fruits");
        if (fruits == NULL) {
            printf("Fruits is NULL\n");
        } else if (fruits->type == cJSON_Array) {
            printf("Fruits: ");
            cJSON *fruit = fruits->child;
            while (fruit != NULL) {
                printf("%s ", fruit->valuestring);
                fruit = fruit->next;
            }
            printf("\n");
        }
    //fruits = cJSON_GetObjectItem(root, "fruits");

    // 打印提取的数据
    //printf("Name: %s\n", name);
    //printf("Name: %s\n", name->valuestring);
    //printf("Age: %d\n", age->valueint);
    //printf("Fruits: %s, %s\n", fruits->child->valuestring, fruits->child->next->valuestring);
    //#endif
    }
    // 保存 JSON 数据到本地
    char *json_string = cJSON_Print(root);
    FILE *output_file = fopen("output.json", "w");
    if (output_file == NULL) {
        perror("Error opening output file");
        exit(EXIT_FAILURE);
    }
    fprintf(output_file, "%s", json_string);
    fclose(output_file);
    free(json_string);
    // 释放 JSON 对象
    cJSON_Delete(root);

    return 0;
}